package server.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JPanel;

import server.control.ServerControl;

import entity.Constants;
import entity.Fish;

public class PoolPanel extends JPanel implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8576776056943701592L;
	
	public ArrayList<Fish> fishes = new ArrayList<Fish>();
	
	private Image imgBG, imgFG;
	private ServerControl serverCtrl;
	
	public PoolPanel(ServerControl server) {
		
		serverCtrl = server;
		
		this.setPreferredSize(new Dimension(Constants.PANEL_P_WIDTH, Constants.PANEL_P_HEIGHT));
		
		URL url = this.getClass().getResource("materials/bg.jpg");
		imgBG = this.getToolkit().getImage(url);
		
		URL url2 = this.getClass().getResource("materials/fg.png");
		imgFG = this.getToolkit().getImage(url2);
	}
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imgBG, 0, 0, Constants.PANEL_P_WIDTH, Constants.PANEL_P_HEIGHT, null);
		
		if(fishes.size() != 0) {
			
			Image img;
			Fish fish;
			for(int i = 0; i < fishes.size(); i++) {
				
				fish  = fishes.get(i);
				
				img = this.getToolkit().getImage(fishes.get(i).url);
				
				g.drawImage(img, fish.x, fish.y, ((int)(fish.oWidth*fish.scale)), ((int)(fish.oHeight*fish.scale)), null);
			}
		}
		
		//draw background
		g.drawImage(imgFG, 0, 0, Constants.PANEL_P_WIDTH, Constants.PANEL_P_HEIGHT, null);
		
		
	}

	@Override
	public void run() {

		while(true) {
			
			serverCtrl.update();
			this.repaint();
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
